//
//  MineTargetWeightTVCell.m
//  Shine
//
//  Created by dllo on 16/3/12.
//  Copyright © 2016年 王斌. All rights reserved.
//

#import "MineTargetWeightTVCell.h"
#import "MineViewController.h"
#define WIDTHCVCELL self.contentView.frame.size.width
#define HEIGHTCVCELL self.contentView.frame.size.height

@interface MineTargetWeightTVCell ()

@end
@implementation MineTargetWeightTVCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCell];
    }
    return self;
}

- (void)createCell{
    
    self.targetButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.targetButton];
    
    self.imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.imageButton];
    
    self.lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(20, 65, WIDTHCVCELL-30, 140)];
    self.lineChart.yLabelFormat = @"%1.1f";
    self.lineChart.backgroundColor = [UIColor clearColor];
    self.lineChart.xLabelColor = [UIColor colorWithWhite:0.902 alpha:1.000];
//    [self.lineChart setXLabels:@[@"", @"", @"", @"", [NSString stringWithFormat:@"%ld周", self.timeInt]]];

    self.lineChart.showCoordinateAxis = YES;
    
    self.lineChart.showLabel = YES;
    
    self.lineChart.yLabelColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    
//    if (self.nowWeightY > self.targetWeightY) {
//        self.lineChart.yFixedValueMax = self.nowWeightY;
//        self.lineChart.yFixedValueMin = self.targetWeightY;
//    } else if (self.nowWeightY < self.targetWeightY) {
//        self.lineChart.yFixedValueMax = self.targetWeightY;
//        self.lineChart.yFixedValueMin = self.nowWeightY;
//    }
//    [self.lineChart setYLabels:@[[NSString stringWithFormat:@"%.1f", self.lineChart.yFixedValueMin], @"", @"", @"", [NSString stringWithFormat:@"%.1f", self.lineChart.yFixedValueMax]]];
//    self.lineChart.yFixedValueMax = 80.0;
//    self.lineChart.yFixedValueMin = 0.0;
//    [self.lineChart setYLabels:@[@"0", @"", @"", @"", [NSString stringWithFormat:@"%.1f", self.lineChart.yFixedValueMax]]];
    //            if ((self.lineChart.yFixedValueMin >= 0.0) && (self.lineChart.yFixedValueMax <= 80.0)) {
    //                [self.lineChart setYLabels:@[@"0", @"20", @"40", @"60", @"80"]];
    //            } else if ((self.lineChart.yFixedValueMin > 80.0) && (self.lineChart.yFixedValueMax <= 160.0)) {
    //                [self.lineChart setYLabels:@[@"80", @"100", @"120", @"140", @"160"]];
    //            } else if ((self.lineChart.yFixedValueMin > 160.0) && (self.lineChart.yFixedValueMax <= 200.0)) {
    //                [self.lineChart setYLabels:@[@"160", @"170", @"180", @"190", @"200"]];
    //            }
    // Line Chart #1
    
//    NSArray * data01Array = @[@(self.targetWeightY), @(self.targetWeightY), @(self.targetWeightY)];
//    PNLineChartData *data01 = [PNLineChartData new];
//    data01.dataTitle = @"Alpha";
//    data01.color = COLORYELLO;
//    data01.alpha = 1;
//    data01.itemCount = data01Array.count;
//    data01.inflexionPointStyle = 4;
//    
//    data01.getData = ^(NSUInteger index) {
//        CGFloat yValue = [data01Array[index] floatValue];
//        return [PNLineChartDataItem dataItemWithY:yValue];
//    };
//    self.lineChart.chartData = @[data01];
//    [self.lineChart strokeChart];
    
    self.Nowline = [[UIView alloc] init];
    self.Nowline.backgroundColor = [UIColor whiteColor];
    [self.lineChart addSubview:self.Nowline];
    
    self.targetLine = [[UIView alloc] init];
    self.targetLine.backgroundColor = COLORYELLO;
    [self.lineChart addSubview:self.targetLine];
    
    [self.contentView addSubview:self.lineChart];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self.targetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(30);
        make.size.mas_equalTo(CGSizeMake(130, 30));
    }];

    [self.imageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(WIDTHCVCELL-60);
        make.top.mas_equalTo(20);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];

    self.Nowline.frame = CGRectMake(35, self.nowWeightY, WIDTHCVCELL-180, 1);
    
    self.targetLine.frame = CGRectMake(35, self.targetWeightY, WIDTHCVCELL-150, 1);
}
// (20, 65, WIDTHCVCELL-30, 140)  27
- (void)setTargetWeightY:(NSInteger)targetWeightY{
    if (_targetWeightY != targetWeightY) {
        _targetWeightY = targetWeightY;
    }
    if (self.nowWeightY > self.targetWeightY) {
                self.lineChart.yFixedValueMin = self.targetWeightY;
                self.lineChart.yFixedValueMax = self.nowWeightY;
            } else if (self.nowWeightY < self.targetWeightY) {
                self.lineChart.yFixedValueMin = self.nowWeightY;
                self.lineChart.yFixedValueMax = self.targetWeightY;
            }
    
    [self.lineChart setYLabels:@[[NSString stringWithFormat:@"%.1f", self.lineChart.yFixedValueMin], @"", @"", @"", [NSString stringWithFormat:@"%.1f", self.lineChart.yFixedValueMax]]];
}

- (void)setNowWeightY:(NSInteger)nowWeightY{
    if (_nowWeightY != nowWeightY) {
        _nowWeightY = nowWeightY;
    }
    if (self.nowWeightY > self.targetWeightY) {
        self.lineChart.yFixedValueMin = self.targetWeightY;
        self.lineChart.yFixedValueMax = self.nowWeightY;
    } else if (self.nowWeightY < self.targetWeightY) {
        self.lineChart.yFixedValueMin = self.nowWeightY;
        self.lineChart.yFixedValueMax = self.targetWeightY;
    }
    [self.lineChart setYLabels:@[[NSString stringWithFormat:@"%.1f", self.lineChart.yFixedValueMin], @"", @"", @"", [NSString stringWithFormat:@"%.1f", self.lineChart.yFixedValueMax]]];
}

- (void)setTimeInt:(NSInteger)timeInt{
    if (_timeInt != timeInt) {
        _timeInt = timeInt;
    }
    
    NSString *XLabelMax = [NSString stringWithFormat:@"%ld天", self.timeInt * 7];
//    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
//    NSDate*nowDate = [NSDate date];
//    NSTimeInterval  interval = 24*60*60*1; //1:天数
//    NSDate*date1 = [nowDate initWithTimeIntervalSinceNow:-interval];
//    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
//    pLabDate.text = [NSString stringWithFormat:@"%@", [dateFormatter stringFromDate:date1]];
    [self.lineChart setXLabels:@[@"", @"", @"", @"", [NSString stringWithFormat:@"%@", XLabelMax]]];
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
